#include "stockrecordmodel.h"

StockRecordModel::StockRecordModel():BaseModel ()
{

}

int StockRecordModel::Table_Query(char *sql)
{
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"search ok";
    }
    else
    {
        qDebug()<<"search no";

    }
    return row;
}

void StockRecordModel::Table_Delete(char *sql)
{
    char *errmsg;
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"search ok";
    }
    else
    {
        qDebug()<<"search no";
    }
}

int StockRecordModel::Table_Inserter(char *sql)
{
    char *errmsg;//接受错误信息
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"inserte ok";
        return 1;
    }
    else
    {
        qDebug()<<"inserte no";

    } return 0;
}

int StockRecordModel::Table_Modifier(char *sql)
{
    char *errmsg;//接受错误信息
    int res;
    res=sqlite3_exec(this->psqlite,sql,nullptr,nullptr,&errmsg);
    if(res==SQLITE_OK)
    {
        qDebug()<<"inserte ok";
        return 1;
    }
    else
    {
        qDebug()<<"inserte no";
        return 0;
    }
}

StockData *StockRecordModel::getStockByName(char *sql)
{
    StockData *tings=nullptr;
    StockData *u[100];
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    //qDebug()<<"0";
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    //qDebug()<<"查询用户数据结束";
    if(row>0&&row<=2)
    {
        //qDebug()<<"1";
        tings=new StockData(atoi(datares[6]),atoi(datares[7]),datares[8],datares[9],atoi(datares[10]),atoi(datares[11]));
        return tings;
    }
    //qDebug()<<"1";
    return tings;
}

QVector<StockData> StockRecordModel::getStock(char *sql)
{
    //获取所有用户信息
    StockData *stock=nullptr;
    addstock.resize(100);//初始化
    //adduser.fill(nullptr);//置零
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    //qDebug()<<"0";
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    qDebug()<<"查询用户数据结束";
     if (row>0)
    {
         //qDebug()<<"row="<<row;
         //把所有数据全都放入adduser中
        for(int i=0;i<row;i++)
        {

          stock=new StockData(atoi(datares[(i+1)*6]),atoi(datares[(i+1)*6+1]),datares[(i+1)*6+2],datares[(i+1)*6+3],atoi(datares[(i+1)*6+4]),atoi(datares[(i+1)*6+5]));
         this->addstock[i]=stock;
         //qDebug()<<"输出用户数据结束";
        }

    }
     return addstock;
}

int StockRecordModel::getRow(char *sql)
{
    StockData *stock=nullptr;
    addstock.resize(100);//初始化
    char *errmsg;//接受错误信息
    char **datares;//接受查询结果集
    int res,row,col;
    //qDebug()<<"0";
    res=sqlite3_get_table(this->psqlite,sql,&datares,&row,&col,&errmsg);
    qDebug()<<"row="<<row;
    return row;
}
